Shader "UGUI/UIMaskGray"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
        _GrayScale ("GrayScale", Float) = 1    
        
            //支持Mask 裁剪的部分
    //Start
    _StencilComp("Stencil Comparison", Float) = 8
    _Stencil("Stencil ID", Float) = 0
    _StencilOp("Stencil Operation", Float) = 0
    _StencilWriteMask("Stencil Write Mask", Float) = 255
    _StencilReadMask("Stencil Read Mask", Float) = 255
    _ColorMask("Color Mask", Float) = 15    
    }

    SubShader
    {
        Tags
        { 
            "Queue"="Transparent" 
            "IgnoreProjector"="True" 
            "RenderType"="Transparent" 
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }
        
        GrabPass
        {
            
        }        

    //支持Mask 裁剪的部分
    //Start
     Stencil
     {
       Ref[_Stencil]
       Comp[_StencilComp]
       Pass[_StencilOp]
       ReadMask[_StencilReadMask]
       WriteMask[_StencilWriteMask]
     }
     ColorMask[_ColorMask]
     //End
     
        Pass
        {
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile _ PIXELSNAP_ON
            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                float4 texcoord  : TEXCOORD0;
            };

            fixed4 _Color;            

            v2f vert(appdata_t IN)
            {                
                v2f OUT;
                OUT.vertex = UnityObjectToClipPos(IN.vertex);
                OUT.texcoord = ComputeGrabScreenPos(OUT.vertex);
                OUT.color = IN.color * _Color;
                #ifdef PIXELSNAP_ON
                OUT.vertex = UnityPixelSnap (OUT.vertex);
                #endif

                return OUT;
            }

            uniform sampler2D _GrabTexture;
            sampler2D _MainTex;
            float _GrayScale;

            fixed4 frag(v2f IN) : SV_Target
            {
                fixed4 col;
                col = tex2Dproj(_GrabTexture, IN.texcoord);
                float bgGrayColor = Luminance(col.rgb);
                col.r = col.g = col.b = bgGrayColor;
                return col;
            }
        ENDCG
        }                        
    }
}
